package com.like.miaosha.rebbitmq;

import com.like.miaosha.domain.MiaoshaOrder;
import com.like.miaosha.domain.MiaoshaUser;
import com.like.miaosha.redis.RedisService;
import com.like.miaosha.result.CodeMsg;
import com.like.miaosha.result.Result;
import com.like.miaosha.service.GoodsService;
import com.like.miaosha.service.MiaoshaService;
import com.like.miaosha.service.MiaoshaUserService;
import com.like.miaosha.service.OrderService;
import com.like.miaosha.vo.GoodsVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @ClassName MQReceiver
 * @Description: TODO
 * @Author dongfengke
 * @Date 2022/1/17
 * @Version V1.0
 **/
@Service
public class MQReceiver {

    @Autowired
    MiaoshaUserService miaoshaUserService;

    @Autowired
    RedisService redisService;

    @Autowired
    GoodsService goodsService;

    @Autowired
    OrderService orderService;

    @Autowired
    MiaoshaService miaoshaService;

    private static Logger log = LoggerFactory.getLogger(MQReceiver.class);
    @RabbitListener(queues = MQConfig.MIAOSHA_QUEUE)
    public void receive(String message){
        log.info("receive message:"+message);
        MiaoshaMessage miaoshaMessage= RedisService.stringToBean(message,MiaoshaMessage.class);
        MiaoshaUser user=miaoshaMessage.getMiaoshaUser();
        long goodsId=miaoshaMessage.getGoodsId();
        GoodsVo goods=goodsService.getGoodsVoByGoodsId(goodsId);
        //判断库存
        Integer stockCount = goods.getStockCount();
        if(stockCount<=0){
            return;
        }
        MiaoshaOrder miaoshaOrder=orderService.getMiaoshaOrderByUserIdGoodsId(user.getId(),goodsId);
        if(miaoshaOrder!=null)  return;
        miaoshaService.miaosha(user,goods);


    }
//    @RabbitListener(queues=MQConfig.QUEUE)
//    public void receive(String message){
//        log.info("receive message:"+message);
//    }
//
//    @RabbitListener(queues=MQConfig.TOPIC_QUEUE1)
//    public void receiveTopic1(String message){
//        log.info("topic queue1 message:"+message);
//    }
//    @RabbitListener(queues=MQConfig.TOPIC_QUEUE2)
//    public void receiveTopic2(String message){
//        log.info("topic queue2 message:"+message);
//    }
//    @RabbitListener(queues = MQConfig.HEADER_QUEUE)
//    public void receiveHeaderQueue(byte[] message){
//        log.info("header queue message:"+new String(message));
//    }
}
